package com.pilot.game.service;

import com.pilot.game.domain.Agency;
import com.pilot.game.repository.AgencyRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing Agency.
 */
@Service
@Transactional
public class AgencyService {

    private final Logger log = LoggerFactory.getLogger(AgencyService.class);

    private final AgencyRepository agencyRepository;

    public AgencyService(AgencyRepository agencyRepository) {
        this.agencyRepository = agencyRepository;
    }

    /**
     * Save a agency.
     *
     * @param agency the entity to save
     * @return the persisted entity
     */
    public Agency save(Agency agency) {
        log.debug("Request to save Agency : {}", agency);
        return agencyRepository.save(agency);
    }

    /**
     *  Get all the agencies.
     *
     *
     * @param example
     * @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<Agency> findAll(Example<Agency> example, Pageable pageable) {
        log.debug("Request to get all Agencies");
        return agencyRepository.findAll(example, pageable);
    }

    /**
     *  Get one agency by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true)
    public Agency findOne(Long id) {
        log.debug("Request to get Agency : {}", id);
        return agencyRepository.findOne(id);
    }

    /**
     *  Delete the  agency by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Agency : {}", id);
        agencyRepository.delete(id);
    }

    public Agency findByNameAndPwd(String accountName, String password) {
        log.debug("Request to findByNameAndPwd Agency : {},{}", accountName, password);
        return agencyRepository.findByAccountNameAndPassword(accountName, password);
    }

    public Agency findByName(String agencyName) {
        log.debug("Request to findByName Agency :{}", agencyName);
        return agencyRepository.findByAccountName(agencyName);
    }


    /**
     * 查询自代理的代理数量
     *
     * @param father father
     * @return result
     */
    public int findSumAgencyByFather(String father) {
        log.debug("Request to findCountByFather father :{}", father);
        return agencyRepository.findSumAgencyByFather(father);
    }
}
